package com.github.prontera.common.auth0.jwt;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import com.github.prontera.common.auth0.jwt.algorithms.Algorithm;
import com.github.prontera.common.auth0.jwt.exceptions.InvalidClaimException;
import com.github.prontera.common.auth0.jwt.exceptions.JWTCreationException;
import com.github.prontera.common.auth0.jwt.exceptions.JWTVerificationException;
import com.github.prontera.common.auth0.jwt.interfaces.Claim;
import com.github.prontera.util.DateTimeUtil;

public class TokenGenerator {

	private static final String secret = "secret";
	private static final String issuer = "issuer";
	private static final int expireHours = 7 * 24;// 7天过期

	public static void main(String[] args) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
		String code="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJpc3N1ZXIiLCJTRVNTSU9OX1VTRVJfSUQiOjEsImV4cCI6MTUxMzEzMDMxOH0.QqXqU0lqELCkuTGbi7X0pqmGjeqnRyJrDGZVWhKvDT8";
		Map<String, Claim>  map=TokenGenerator.getClaims(code);
		for (String claim : map.keySet()) {
			System.out.println(claim);
		}
	}
//	
//	public static void main(String[] args) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
//		Map<String, Object> claims = new HashMap<String, Object>();
//		claims.put(MagicConstants.SESSION_USER_ID,1);
//		String tokenString = JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addSeconds(DateTimeUtil.getSystemDate(), 10)).withClaim(claims)
//				.sign(Algorithm.HMAC256(secret));
//		System.out.println(tokenString);
//	}
	
	
	public static Token create(Map<String, Object> claims) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
		String tokenString = JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addHours(DateTimeUtil.getSystemDate(), expireHours)).withClaim(claims)
				.sign(Algorithm.HMAC256(secret));
		return new Token(tokenString, expireHours * 60 * 60);
	}

	public static String create(int expiresAt, Map<String, Object> claims) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
		return JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addSeconds(DateTimeUtil.getSystemDate(), expiresAt)).withClaim(claims)
				.sign(Algorithm.HMAC256(secret));
	}

	public static Claim getClaim(String token, String claim) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
		return JWT.decode(token).getClaim(claim);
	}

	public static Map<String, Claim> getClaims(String token) throws IllegalArgumentException, JWTCreationException, UnsupportedEncodingException {
		return JWT.decode(token).getClaims();
	}

	public static boolean isExpired(String token) throws JWTVerificationException, IllegalArgumentException, UnsupportedEncodingException {
		try {
			return DateTimeUtil.compareTwoDate(DateTimeUtil.getSystemDate(), JWT.require(Algorithm.HMAC256(secret)).withIssuer(issuer).build().verify(token)
					.getExpiresAt()) < 0 ? true : false;
		} catch (InvalidClaimException e) {
			return false;
		}
	}

	public static String create(String secret, String issuer, int expiresAt, Map<String, Object> claims) throws IllegalArgumentException, JWTCreationException,
			UnsupportedEncodingException {
		return JWT.create().withIssuer(issuer).withExpiresAt(DateTimeUtil.addSeconds(DateTimeUtil.getSystemDate(), expiresAt)).withClaim(claims)
				.sign(Algorithm.HMAC256(secret));
	}

	public static boolean isExpired(String secret, String issuer, String token) throws JWTVerificationException, IllegalArgumentException,
			UnsupportedEncodingException {
		return DateTimeUtil.compareTwoDate(DateTimeUtil.getSystemDate(), JWT.require(Algorithm.HMAC256(secret)).withIssuer(issuer).build().verify(token)
				.getExpiresAt()) > 0 ? true : false;
	}

}
